<?php

namespace App\Http\Resources\panyuan;

use App\model\UserWorkshop;
use App\model\Workshop;
use App\Repository\WorkShopRepository;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;

class WorkshopDetailResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if ($this->type == 0) {
        $price = $this->rent != 0 ? $this->rent . Workshop::RENT : '面议';
    } else {
        $price = $this->price != 0 ? $this->rent . Workshop::SALE : '面议';
    }

        return [
            'code' => 'success',
            'message' => '请求成功',
            'data' => [
                'workshop_id' => $this->id,
                'title' => $this->title,
                'logo'=>$this->logo,
                'industry'=>$this->industry->reject(function($item,$key){
                    return $key>1;
                }),
                'area' => $this->area,
                'qu' => $this->qu->title ?? '' ,
                'zhen' => $this->zhen->title ?? '' ,
                'slider' => collect(json_decode($this->slider, true))->reject(function ($value) {
                    return $value['isDisplay'] == 0;
                })->map(function ($value) {
                    return WorkShopRepository::getSlider($value['img']);
                   // return $value['img'];
                }),//不显示slider
                'price' => $price,
                'is_guoqi'=>Carbon::parse($this->commission_end_day)->lt(Carbon::now()->addDays(30)) ? 1 :0,
                'is_rent' => $this->is_rent==0 ? '不是':'是',
                'hangye' => $this->hangye->map(function($item){
                    return [
                        'name'=>$item->name,
                        'icon'=>Storage::url($item->icon),
                    ];
                    }),
                'desc' => $this->owndesc,
                'high' => $this->high,
                'commission_end_day'=>date('Y年m月d日',strtotime($this->commission_end_day)),
                'commission_start_day'=>date('Y年m月d日',strtotime($this->commission_start_day)),
                'floor' => $this->floor==0  ?  '整栋' : $this->floor.'层',
                'elevator' => $this->elevator,
                'lat' => $this->latitude,
                'lng' => $this->longitude,
                'inside_desc'=>$this->inside_desc,
                'distribution' => $this->distribution !==0 ? $this->distribution.'KW' : '面议',
                'certificates' => Workshop::$certificatesMap[$this->certificates] ?? '',
                'weight' => $this->weight_type->name ?? $this->weight.'kg',
                'address'=>$this->address,
                'is_rent'=>Workshop::$rentMap[$this->is_rent ?? 0],
            ]
        ];
    }
}
